import * as sqlite_opt from "@/utils/sqliteUtil.js";

const thickBookTableName = "t_thck_book";

export const requireThickBookDataByDmgCode = (inDmgCode) => {
	return new Promise((resolve, reject) => {
		let sqlStr = `SELECT * `;
	
		sqlStr += `from ` + thickBookTableName;
		sqlStr += ` where `;
		sqlStr += "   1 = 1 "
		sqlStr += ` AND dmg_code = ` + inDmgCode;
		
		sqlStr += `  ORDER BY book_no`;
		
		console.log("requireThickBookDataByDmgCode")
		console.log(sqlStr)

		sqlite_opt.selectSql(sqlStr,
			(e) => {
				resolve(e);
			},
			(e) => {
				console.log(e)
				reject(e);
			});
	})
}

/**
 * inDmgCode 机器编号
 * inBookNo 野账序号
 * inBookTil 分类标题
 * inPass 文件路径
 * inIoFlag  0:A-MIS→LOG-BOOK / 1:A-MIS←LOG-BOOK
 * inPltName 装置名
 * inMacType 机器or配管
 * inIsFldBook  0: 野帳以外/ 1:野帳
 * inShotPath 图纸截图path
 * inContent 输入的内容
 * inCreateTime 创建日期
 * inFileName 文件名
 */
export const createThickBookData = (inObj) => {
	let keys = Object.keys(inObj)
	let keyStr = keys.toString()
	let valStr = ''
	keys.forEach((item, index) => {
		valStr += inObj[item] ? ('"' + inObj[item] + '"') : null;
	
		if (keys.length - 1 != index)
			valStr += ","
	})
	
	return new Promise((resolve, reject) => {
		let sqlStr = `insert into ${thickBookTableName} (${keyStr}) values(${valStr})`;
		sqlite_opt.selectSql(sqlStr,
			(e) => {
				resolve(e);
			},
			(e) => {
				console.log(e)
				reject(e);
			});
	})
}

export const deleteThickBookData = (inDmgCode) => {
	return new Promise((resolve, reject) => {
		let sqlStr = `delete from ${thickBookTableName} where dmg_code = ${inDmgCode}`;

		sqlite_opt.executeSql(sqlStr,
			(e) => {
				resolve(e);
			},
			(e) => {
				console.log(e)
				reject(e);
			});
	})
}

export const updateThickBookData = (inDmgId, inObj) => {
	let keys = Object.keys(inObj)
	let keyStr = keys.toString()
	let valStr = ''
	keys.forEach((item , index) => {
		valStr += item + `  =  "`;
		valStr += inObj[item] + `" ` ;
		
		if (keys.length - 1 != index)
			valStr += ","
	})
	return new Promise((resolve, reject) => {
		let sqlStr = `update ${thickBookTableName} set ${valStr} where dmg_code = "${inDmgId}"`;
		console.log("updateLoginAccount")
		console.log(sqlStr)
		sqlite_opt.executeSql(sqlStr,
			(e) => {
				resolve(e);
			},
			(e) => {
				console.log(e)
				reject(e);
			});
	})
}